<script type="text/javascript">
	$j("span.action").live("click", function(){doAction($j(this));});

	function doAction(actionSpan){
		var actionName = actionSpan.attr('action');
		
		switch(actionName){
		case 'connection' :
			connection(actionSpan);
			break;
		case 'deconnection' :
			deconnection(actionSpan);
			break;
		case 'subscribeValidation' :
			subscribeValidation(actionSpan);
			break;
		case 'closeLoginboxAction' :
			closeLoginboxAction();
		  	break;
		case 'openSubscriptionModule':
			openSubscriptionModule(actionSpan);
		  	break;
		case 'openLostPasswordModule':
			openLostPasswordModule(actionSpan);
		  	break;
		case 'moreFeedOption':
			moreFeedOption(actionSpan);
		  	break;
		case 'onoffFeed':
			onoffFeed(actionSpan);
		  	break;
		case 'feed_viewNew':
			feed_viewNew(actionSpan);
			break;
		case 'feed_viewOld':
			feed_viewOld(actionSpan);
			break;
		case 'openFooterModule':
			openFooterModule(actionSpan);
			break;
		case 'closeFooterModule':
			closeFooterModule(actionSpan);
			break;
		case 'openTalk' :
			openTalk(actionSpan);
			break;
		case 'opencloseBetComment' :
			opencloseBetComment(actionSpan);
			break;			
		case 'displayAllComments' :
			displayAllComments(actionSpan);
			break;
		case 'displayMoreFriendSuggestion' :
			displayMoreFriendSuggestion(actionSpan);
			break;
		case 'getEventDetails' :
			getEventDetails(actionSpan);
			break;
			
		default:
			alert(actionName +' : fonction non définie');
		}
	};

	function connection(actionSpan){
		var mail = $j("#ipt_connectionMail").val();
		var password = $j("#ipt_connectionPassword").val();
		
		// traitement 
		$j.ajax({
			type : "POST",
			url : "<?php bloginfo('template_url'); ?>/ajax/doAction.php",
			data : "actionName=connection&mail="+mail+"&password="+password,
			success:function(response){			
				response = JSON.parse(response);
				
				$j("#ipt_connectionMail").removeClass('error');
				$j("#ipt_connectionPassword").removeClass('error');
				
				if(response['result'] == false){
					$j("#"+response['message']).addClass('error');
				}else{
					$j("#ipt_connectionMail").addClass('valid');
					$j("#ipt_connectionPassword").addClass('valid');

					window.location.reload();
				} 
			}
		});
	}

	function deconnection(actionSpan){
		$j.ajax({
			type : "POST",
			url : "<?php bloginfo('template_url'); ?>/ajax/doAction.php",
			data : "actionName=deconnection",
			success:function(response){				
				window.location.reload();
			}
		});
	}
	function subscribeValidation(actionSpan){
		var nickname = $j("#ipt_subscribeNickname").val();
		var mail = $j("#ipt_subscribeMail").val();
		var password = $j("#ipt_subscribePassword").val();

		// traitement 
		$j.ajax({
			type : "POST",
			url : "<?php bloginfo('template_url'); ?>/ajax/doAction.php",
			data : "actionName=subscribe&nickname="+nickname+"&mail="+mail+"&password="+password,
			success:function(response){
				response = JSON.parse(response);
				if(response['result'] == false){
					$j("#subscribeMessages").addClass('error');
					$j("#subscribeMessages").slideDown();
					$j("#subscribeMessages").html(response['message']);
				}else{
					$j("#subscribeMessages").removeClass('error');
					$j("#subscribeMessages").addClass('valid');
					$j("#subscribeMessages").slideDown().fadeIn();
					$j("#subscribeMessages").html("Merci, un email de confirmation vous à été envoyé");

					setTimeout("closeLoginboxAction()", 5000);
				} 
			}
		});
	}
	
	function closeLoginboxAction(){
		// fermeture de tous les onglets
		$j('#div_login > #div_bottom > span.action').removeClass('open');
		var target = $j("#div_loginboxAction");
		target.slideUp();
	}
	
	function openSubscriptionModule(actionSpan){

		if(!actionSpan.is('.open')){
			closeLoginboxAction();
			
			var target = $j("#div_loginboxAction");
	
			// chargement du module
			$j.ajax({
				type : "POST",
				url : "<?php bloginfo('template_url'); ?>/ajax/getContent.php",
				data : "functionName=getSubscribeModule",
				success:function(response){
					target.html(response);
					actionSpan.addClass('open');
					target.slideDown();
				}
			});
		}	

	}

	function openLostPasswordModule(actionSpan){

		if(!actionSpan.is('.open')){
			closeLoginboxAction();
			
			var target = $j("#div_loginboxAction");
	
			// chargement du module
			$j.ajax({
				type : "POST",
				url : "<?php bloginfo('template_url'); ?>/ajax/getContent.php",
				data : "functionName=getLostPasswordModule",
				success:function(response){
					target.html(response);
					actionSpan.addClass('open');
					target.slideDown();
				}
			});
		}	
	}

	function onoffFeed(actionSpan){
		option = actionSpan.attr('option');

		if(actionSpan.is('.disable')){
			actionSpan.removeClass('disable');
		}else{
			actionSpan.addClass('disable');
		}

		$j("."+option).each(function(){
			$j(this).slideToggle();
		});
	}
	
	function moreFeedOption(actionSpan){
		var optionDiv = actionSpan.parent();
		if(optionDiv.is('.open')){
			actionSpan.html("+");
			optionDiv.removeClass('open');
			optionDiv.children().each( function(){
				if($j(this).is('.hideable')) $j(this).addClass('hide');
			});
		}else{
			actionSpan.html("-");
			optionDiv.addClass('open');
			optionDiv.children().each( function(){
				$j(this).removeClass('hide');
			});
		}
	}

	function feed_viewNew(actionSpan){
		var target = $j(actionSpan.attr("target"));
		var minID = parseInt(actionSpan.attr('minID'));

		// mise a jour du minID
		$j.ajax({
			type : "POST",
			url : "<?php bloginfo('template_url'); ?>/ajax/getValue.php",
			data : "functionName=getLastPublicationID",
			success:function(response){
				actionSpan.attr('minID',response);
			}
		});
		
		// chargement des publications
		$j.ajax({
			type : "POST",
			url : "<?php bloginfo('template_url'); ?>/ajax/getContent.php",
			data : "functionName=getFeedContent&minID="+minID,
			success:function(response){
				target.prepend(response);
			}
		});	

		
	}

	function feed_viewOld(actionSpan){
		var maxID = parseInt(actionSpan.attr('maxID'));
		$j("#feed_feed").append("<div class='fl'>apres "+maxID+" </div>");

		actionSpan.attr('maxID',maxID+1);
	}


	function closeFooterModule(){
		// fermeture de tous les onglets
		$j('footer > #div_content > #div_left .div_item').removeClass('open');
		$j('footer > #div_content > #div_left span.action').removeClass('open');
		var target = $j("footer > #div_content > #div_left > #div_content");
		target.slideUp();
	}
	
	function openFooterModule(actionSpan){
		
		if(!actionSpan.is('.open')){
			
			closeFooterModule();
			
			var target = $j("footer > #div_content > #div_left > #div_content");
			var functionName = actionSpan.attr('option');
			// chargement du module 
			$j.ajax({
				type : "POST",
				url : "<?php bloginfo('template_url'); ?>/ajax/getContent.php",
				data : "functionName="+functionName,
				success:function(response){
					target.html(response);
					actionSpan.addClass('open');
					actionSpan.parent().addClass('open');
					target.slideDown();
				}
			});
		}
	}

	function openTalk(actionSpan){
		var target = $j("#div_tchatTalkContent");
		var talkTo = actionSpan.attr('talkTo');
		$j.ajax({
			type : "POST",
			url : "<?php bloginfo('template_url'); ?>/ajax/getContent.php",
			data : "functionName=getTalkContent&talkTo="+talkTo,
			success:function(response){
				target.attr('talkTo', talkTo);
				target.html(response);
				actionSpan.addClass('open');
			}
		});
	}

	function opencloseBetComment(actionSpan){
		var option = actionSpan.attr('option');
		if(option == 'open'){
			var ticketLine = actionSpan.attr('ticketLine');
			$j.ajax({
				type : "POST",
				url : "<?php bloginfo('template_url'); ?>/ajax/getContent.php",
				data : "functionName=getLineTicketComment&ticketLine="+ticketLine,
				success:function(response){
					actionSpan.attr('option', 'close');
					actionSpan.removeClass('seeComment');
					actionSpan.addClass('hideComment');
					actionSpan.before(response);
				}
			});
		}else{
			actionSpan.prev().remove();
			actionSpan.attr('option', 'open');
			actionSpan.removeClass('hideComment');
			actionSpan.addClass('seeComment');
		}
	}

	function displayAllComments(actionSpan){
		var target = actionSpan.parent();

		var publicationType = actionSpan.attr('publicationType');
		var publicationID = actionSpan.attr('publicationID');
		
		// suppression du commentaire afficher par défaut
		target.next().remove();

		// chargement du contenu
		$j.ajax({
			type : "POST",
			url : "<?php bloginfo('template_url'); ?>/ajax/getContent.php",
			data : "functionName=getAllComments&publicationType="+publicationType+"&publicationID="+publicationID+"",
			success:function(response){
				target.slideUp();
				target.after(response);
				target.remove();
			}
		});		
	}

	function displayMoreFriendSuggestion(actionSpan){
		var nbSuggestion = actionSpan.attr('nbSuggestion');
		var targetDiv = $j(actionSpan.attr('targetDiv'));

		$j.ajax({
			type : "POST",
			url : "<?php bloginfo('template_url'); ?>/ajax/getContent.php",
			data : "functionName=getFriendSuggestionModule&nbSuggestion="+nbSuggestion,
			success:function(html){
				targetDiv.html(html);
			}
		});	
	}

	function closeAllEventDetails(){
		$j(".div_league ul li.open").each(function(){
			$j(this).next().slideToggle();
		});

		$j(".div_league .open").each(function(){
			$j(this).removeClass("open");
		});
	}
	
	function getEventDetails(actionSpan){
		var event = actionSpan.attr('eventID');
		var target = actionSpan.parent();
		if(!target.is('.open')){
			closeAllEventDetails();
			target.addClass('open');
			actionSpan.addClass('open');
			if(!target.is('.loaded') ){
				target.addClass('loaded');
				$j.ajax({
					type : "POST",
					url : "<?php bloginfo('template_url'); ?>/ajax/getContent.php",
					data : "functionName=getEventDetails&eventID="+event,
					success:function(html){
						target.after(html);
						target.next().slideToggle();
					}
				});	
			}else{
				target.next().slideToggle();
			}
		}else{
			target.removeClass('open');
			actionSpan.removeClass('open');
			target.next().slideToggle();
		}
	}
</script>

